package com.sdjictec.haier.utils;

import com.sdjictec.haier.config.ProFileConfig;
import com.sdjictec.haier.config.StaticCode;
import com.sdjictec.haier.entity.RetailDataMQ;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

/**
 * @author 吴敬超
 * @date 2018/8/13
 * Description:
 */
public class IJMSInfo {


    protected static Logger logger = LoggerFactory.getLogger(IJMSInfo.class);


    public RetailDataMQ parseJson(Map<String, Object> billData) {
        RetailDataMQ retailDataMQ = new RetailDataMQ();
        //单据唯一编码
        String billUniCode = (String) billData.get("billUniCode");
        retailDataMQ.setBillUniCode(billUniCode);
        //主售达方编码
        String spCode = (String) billData.get("spCode");
        retailDataMQ.setSpCode(spCode);
        //门店唯一码
        String shUniCode = (String) billData.get("shUniCode");
        retailDataMQ.setShUniCode(shUniCode);
        //门店名称
        String shName = (String) billData.get("shName");
        retailDataMQ.setShName(shName);
        //门店地址
        String shAddress = (String) billData.get("shAddress");
        retailDataMQ.setShAddress(shAddress);
        //门店联系人
        String shLinkMan = (String) billData.get("shLinkMan");
        retailDataMQ.setShLinkMan(shLinkMan);
        //单据类型编码
        String billTypeCode = (String) billData.get("billTypeCode");
        retailDataMQ.setBillTypeCode(billTypeCode);
        //生成单据时间
        String orderDate = (String) billData.get("orderDate");


        try {
            String orderAccDate = (String) billData.get("orderAccDate");
            if (orderAccDate != null) {
                orderDate = orderAccDate;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            Date parse = formatter.parse(orderDate);
            retailDataMQ.setOrderDate(new Timestamp(parse.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }


        //商品编码
        String productCode = (String) billData.get("productCode");
        retailDataMQ.setProductCode(productCode);
        //商品数量

        Integer amount = 0;

        try {
            amount = (Integer) billData.get("amount");
        } catch (Exception e) {

            try {
                BigDecimal bdAmount = new BigDecimal(billData.get("amount") + "");

                amount = bdAmount.intValue();

                logger.error("BigDecimal:" + billData.get("amount"));
            } catch (Exception e1) {
                e1.printStackTrace();
            }


            e.printStackTrace();
        }
        retailDataMQ.setAmount(0 - amount);
        //品牌编码
        String brandCode = (String) billData.get("brandCode");
        retailDataMQ.setBrandCode(brandCode);


        return retailDataMQ;
    }

    public boolean filterFlag(Map<String, Object> mapTypes, Map<String, Object> billData) {
        try {
            Properties props = new Properties();

            String billTypeCode = (String) billData.get("billTypeCode");
            String orderDate = (String) billData.get("orderDate");


//            if (!isTodayINFO(orderDate))
//                return false;

            String flag = (String) mapTypes.get("flag");

            if (flag.equals(ProFileConfig.realtimeFlagFilter) &&
                    (StaticCode.filterBillTypeCOde.contains(billTypeCode))) {

                return true;

            } else {

                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


}
